package uit.qlks.from;

import java.sql.ResultSetMetaData;
import java.util.Vector;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.sql.rowset.WebRowSet;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
import uit.ejb.dto.LoaiDichVuDTO;
import uit.qlks.baocao.BaoCao;
import uit.qlks.controller.loaidichvu.LoaiDichVuController;


public class frmDichVu extends javax.swing.JFrame {
    private static int flag = 0;
    private Vector rowdata;
    public frmDichVu() {
        initComponents();
    }
   private void LayDanhSachDichVu() {
        try{
            WebRowSet wrs = LoaiDichVuController.loaiDichVu_GetByAll();
            String[] tableColumnsName = {"Mã Dịch Vụ","Tên Dịch Vụ", "Giá (VNĐ)"};
            this.UpdateTable(this.jtbDanhSachDV, tableColumnsName, wrs);
        }
        catch(Exception e)
        {
           JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnSua = new javax.swing.JButton();
        btnXoa = new javax.swing.JButton();
        btnThem = new javax.swing.JButton();
        txtGia = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtDichVu = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jtbDanhSachDV = new javax.swing.JTable();
        jLabel7 = new javax.swing.JLabel();
        btnPrint = new javax.swing.JButton();
        btnLuu = new javax.swing.JButton();
        btnTroLai = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Quản Lý Dịch Vụ");
        setMaximizedBounds(new java.awt.Rectangle(0, 0, 0, 0));
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosed(java.awt.event.WindowEvent evt) {
                formWindowClosed(evt);
            }
        });
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jPanel1.setPreferredSize(new java.awt.Dimension(864, 507));

        btnSua.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit.png"))); // NOI18N
        btnSua.setText("Sửa");
        btnSua.setToolTipText("Sửa");
        btnSua.setBorder(null);
        btnSua.setContentAreaFilled(false);
        btnSua.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Edit2.png"))); // NOI18N
        btnSua.setVerifyInputWhenFocusTarget(false);
        btnSua.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSuaActionPerformed(evt);
            }
        });

        btnXoa.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Delete.png"))); // NOI18N
        btnXoa.setText("Xóa");
        btnXoa.setToolTipText("Xóa");
        btnXoa.setBorder(null);
        btnXoa.setContentAreaFilled(false);
        btnXoa.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Delete2.png"))); // NOI18N
        btnXoa.setVerifyInputWhenFocusTarget(false);
        btnXoa.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnXoaActionPerformed(evt);
            }
        });

        btnThem.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Add.png"))); // NOI18N
        btnThem.setText("Thêm");
        btnThem.setToolTipText("Thêm");
        btnThem.setBorder(null);
        btnThem.setContentAreaFilled(false);
        btnThem.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Add2.png"))); // NOI18N
        btnThem.setVerifyInputWhenFocusTarget(false);
        btnThem.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnThemActionPerformed(evt);
            }
        });

        txtGia.setEnabled(false);
        txtGia.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtGiaKeyTyped(evt);
            }
        });

        jLabel3.setText("Giá DV:");

        txtDichVu.setEnabled(false);

        jLabel2.setText("Tên DV:");

        jLabel1.setFont(new java.awt.Font("Times New Roman", 1, 18));
        jLabel1.setText("DANH SÁCH DỊCH VỤ");

        jtbDanhSachDV.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        jtbDanhSachDV.setFont(new java.awt.Font("Times New Roman", 0, 14));
        jtbDanhSachDV.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Mã Dịch Vụ", "Tên Dịch Vụ", "Giá"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jtbDanhSachDV.getTableHeader().setReorderingAllowed(false);
        jtbDanhSachDV.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jtbDanhSachDVMousePressed(evt);
            }
        });
        jtbDanhSachDV.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                jtbDanhSachDVKeyPressed(evt);
            }
        });
        jScrollPane1.setViewportView(jtbDanhSachDV);

        jLabel7.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/service.png"))); // NOI18N

        btnPrint.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Printer.png"))); // NOI18N
        btnPrint.setText("Print");
        btnPrint.setBorder(null);
        btnPrint.setContentAreaFilled(false);
        btnPrint.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Printer2.png"))); // NOI18N
        btnPrint.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnPrintActionPerformed(evt);
            }
        });

        btnLuu.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Save.png"))); // NOI18N
        btnLuu.setText("Lưu");
        btnLuu.setToolTipText("Lưu");
        btnLuu.setBorder(null);
        btnLuu.setContentAreaFilled(false);
        btnLuu.setEnabled(false);
        btnLuu.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/Save2.png"))); // NOI18N
        btnLuu.setVerifyInputWhenFocusTarget(false);
        btnLuu.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLuuActionPerformed(evt);
            }
        });

        btnTroLai.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/back.png"))); // NOI18N
        btnTroLai.setText("Trở lại");
        btnTroLai.setToolTipText("Trở lại");
        btnTroLai.setBorder(null);
        btnTroLai.setContentAreaFilled(false);
        btnTroLai.setEnabled(false);
        btnTroLai.setRolloverIcon(new javax.swing.ImageIcon(getClass().getResource("/Image/back2.png"))); // NOI18N
        btnTroLai.setVerifyInputWhenFocusTarget(false);
        btnTroLai.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnTroLaiActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(21, 21, 21)
                                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addGap(3, 3, 3)
                                        .addComponent(jLabel3)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtGia, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGroup(jPanel1Layout.createSequentialGroup()
                                        .addComponent(jLabel2)
                                        .addGap(18, 18, 18)
                                        .addComponent(txtDichVu, javax.swing.GroupLayout.PREFERRED_SIZE, 153, javax.swing.GroupLayout.PREFERRED_SIZE))))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(51, 51, 51)
                                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(18, 18, 18)
                                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 409, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(125, 125, 125)
                                .addComponent(jLabel1))))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(btnThem)
                        .addGap(26, 26, 26)
                        .addComponent(btnXoa)
                        .addGap(26, 26, 26)
                        .addComponent(btnSua)
                        .addGap(39, 39, 39)
                        .addComponent(btnLuu)
                        .addGap(32, 32, 32)
                        .addComponent(btnTroLai)
                        .addGap(40, 40, 40)
                        .addComponent(btnPrint)))
                .addContainerGap(22, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 248, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 127, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(30, 30, 30)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel2))
                            .addComponent(txtDichVu, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanel1Layout.createSequentialGroup()
                                .addGap(3, 3, 3)
                                .addComponent(jLabel3))
                            .addComponent(txtGia, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addGap(23, 23, 23)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnThem)
                    .addComponent(btnXoa)
                    .addComponent(btnSua)
                    .addComponent(btnLuu)
                    .addComponent(btnPrint)
                    .addComponent(btnTroLai))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 679, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(49, 49, 49))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 354, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-704)/2, (screenSize.height-414)/2, 704, 414);
    }// </editor-fold>//GEN-END:initComponents

private void jtbDanhSachDVMousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jtbDanhSachDVMousePressed
    try{
        this.txtDichVu.setText(jtbDanhSachDV.getValueAt(this.jtbDanhSachDV.getSelectedRow(), 1).toString());
        this.txtGia.setText(jtbDanhSachDV.getValueAt(this.jtbDanhSachDV.getSelectedRow(), 2).toString().replace(".", ""));
        this.btnSua.setEnabled(true);
        this.btnXoa.setEnabled(true);
        //this.btnThem.setEnabled(false);
    }
    catch(Exception e)
    {
        
    }
}//GEN-LAST:event_jtbDanhSachDVMousePressed

private void btnThemActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnThemActionPerformed
    this.txtDichVu.setEnabled(true);
    this.txtDichVu.setText("");
    this.txtGia.setEnabled(true);
    this.txtGia.setText("");
    this.btnThem.setEnabled(false);
    this.btnXoa.setEnabled(false);
    this.btnSua.setEnabled(false);
    this.btnLuu.setEnabled(true);
    this.btnTroLai.setEnabled(true);
    flag = 2;
}//GEN-LAST:event_btnThemActionPerformed

private void btnXoaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnXoaActionPerformed
        int i = this.jtbDanhSachDV.getSelectedRow();
        if(i==-1)
        {
            JOptionPane.showMessageDialog(this, "Bạn vui lòng chọn Dịch Vụ để thao tác !!");
            return;
        }
        try{
            if(JOptionPane.showConfirmDialog(this, "Bạn có chắc chắn  muốn xóa không ?", "Question",JOptionPane.YES_NO_OPTION)!=1)
            {
                int[] a = this.jtbDanhSachDV.getSelectedRows();
                for(int t = 0; t< a.length; t++)
                {
                    if(!LoaiDichVuController.loaiDichVu_Delete(jtbDanhSachDV.getValueAt(a[t], 0).toString()))
                    {
                        JOptionPane.showMessageDialog(rootPane, "Loại Phòng đang được sử dụng - không thể xóa.", "Warning", JOptionPane.WARNING_MESSAGE);
                        this.LayDanhSachDichVu();
                        return;
                    }
                }
                this.LayDanhSachDichVu();
                JOptionPane.showMessageDialog(this, "Xóa thành công !!");
            }
            else
            {
                return;
            }
        }
        catch(Exception e)
        {
           JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
        this.txtDichVu.setText("");
        this.txtGia.setText("");
        this.btnSua.setEnabled(false);
        this.btnXoa.setEnabled(false);
        this.btnThem.setEnabled(true);
}//GEN-LAST:event_btnXoaActionPerformed

private void btnSuaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSuaActionPerformed
        
    int i = this.jtbDanhSachDV.getSelectedRow();
    if(i == -1)
    {
        JOptionPane.showMessageDialog(this, "Xin vui lòng chọn dịch vụ cần sửa");
    }
    else
    {
        this.btnThem.setEnabled(false);
        this.btnXoa.setEnabled(false);
        this.btnLuu.setEnabled(true);
        this.btnTroLai.setEnabled(true);
        this.txtDichVu.setEnabled(true);
        this.txtGia.setEnabled(true);
        flag = 1;
    }  
}//GEN-LAST:event_btnSuaActionPerformed

private void txtGiaKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtGiaKeyTyped
    char c = evt.getKeyChar();
    if(!Character.isDigit(c) )
    {
        evt.consume();
    }
}//GEN-LAST:event_txtGiaKeyTyped

private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
    try{
            this.LayDanhSachDichVu();
            this.btnSua.setEnabled(false);
            this.btnXoa.setEnabled(false);

    }
    catch(Exception e){
        JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
    }
}//GEN-LAST:event_formComponentShown

private void jtbDanhSachDVKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_jtbDanhSachDVKeyPressed
    try{
        this.txtDichVu.setText(jtbDanhSachDV.getValueAt(this.jtbDanhSachDV.getSelectedRow(), 1).toString());
        this.txtGia.setText(jtbDanhSachDV.getValueAt(this.jtbDanhSachDV.getSelectedRow(), 2).toString());
        this.btnSua.setEnabled(true);
        this.btnXoa.setEnabled(true);
        this.btnThem.setEnabled(false);
    }
    catch(Exception e)
    {
        
    }
        

}//GEN-LAST:event_jtbDanhSachDVKeyPressed

private void formWindowClosed(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosed
    frmMain._main.setVisible(true);
    frmMain._main.setFocusable(true);
    // TODO add your handling code here:
}//GEN-LAST:event_formWindowClosed

private void btnPrintActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnPrintActionPerformed

    try {
            // TODO add your handling code here:
            String html1 = "";//<meta http-equiv=Content-Type content='text/html; charset=utf8'>";
            //html1 += "<LEFT>CỘNG HÒA XÃ HỘI CHỦ NGHĨA VIỆT NAM</BR>----------Ðộc lập - Tự do - Hạnh phúc-----------</LEFT>";
            html1 += "<CENTER><h2>DANH SÁCH DỊCH VỤ</h2>"; 
//            html1 += "&nbsp;&nbsp;&nbsp<b>Năm : </b>" + ycNam.getYear();

            String footer ="";
//            footer += "<b>Tiền phòng : </b>" + lbTienPhong.getText() + " VNĐ<br>";
//            footer += "&nbsp;&nbsp;&nbsp<b>Tiền dịch vụ : </b>" + lbTienDV.getText() + " VNĐ<br>";
//            footer += "&nbsp;&nbsp;&nbsp<b>Tổng doanh thu : </b>" + lbDoanhThu.getText() + " VNĐ<br>";
            BaoCao bc = new BaoCao();
            bc.baocao(jtbDanhSachDV,html1,footer,"400");
        } catch (Exception ex) {
            Logger.getLogger(frmBaoCao.class.getName()).log(Level.SEVERE, null, ex);
        }

    // TODO add your handling code here:
}//GEN-LAST:event_btnPrintActionPerformed

private void btnLuuActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLuuActionPerformed
    // TODO add your handling code here:
    if("".equals(txtDichVu.getText()))
    {
        JOptionPane.showMessageDialog(this, "Tên dịch vụ không được để trống !");
        return;
    }
    else
        if("".equals(txtGia.getText()))
        {
            JOptionPane.showMessageDialog(this, "Giá dịch vụ không được để trống !");
            return;
        }
    if(flag == 1)//Sửa
    {
        LoaiDichVuDTO ldv = new LoaiDichVuDTO();
        ldv.setMaDV(jtbDanhSachDV.getValueAt(this.jtbDanhSachDV.getSelectedRow(), 0).toString());
        ldv.setTenDV(this.txtDichVu.getText().toString());
        ldv.setGia(this.txtGia.getText().toString());
        try{
            if(JOptionPane.showConfirmDialog(this, "Bạn có chắc chắn  muốn sửa không ?", "Question",JOptionPane.YES_NO_OPTION)!=1)
            {
                if(LoaiDichVuController.loaiDichVu_Update(ldv)){
                    JOptionPane.showMessageDialog(this, "Sửa thành công !!");
                    this.LayDanhSachDichVu();
                    flag = 0;
                    this.btnThem.setEnabled(true);
                    this.btnXoa.setEnabled(true);
                    this.btnLuu.setEnabled(false);
                    this.btnTroLai.setEnabled(false);
                    txtDichVu.setEnabled(false);
                    txtGia.setEnabled(false);
                }
            }
            else
            {
                return;
            }
        }
        catch(Exception e)
        {
             JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    if(flag == 2)//Thêm
    {
        LoaiDichVuDTO loaidv = new LoaiDichVuDTO();
        loaidv.setTenDV(this.txtDichVu.getText().toString());
        loaidv.setGia(this.txtGia.getText().toString());
        try{
            if(LoaiDichVuController.loaiDichVu_Insert(loaidv)){
                JOptionPane.showMessageDialog(this, "Thêm Thành Công !!");
                this.LayDanhSachDichVu();
                flag = 0;
                this.txtDichVu.setEnabled(false);
                this.txtGia.setEnabled(false);
                this.btnThem.setEnabled(true);
                this.btnXoa.setEnabled(true);
                this.btnSua.setEnabled(true);
                this.btnLuu.setEnabled(false);
                this.btnTroLai.setEnabled(false);
            }
        }
        catch(Exception e)
        {
            JOptionPane.showMessageDialog(new JFrame(), "Đường truyền bị gián đoạn ! Xin vui lòng kiểm tra lại", "Test Connect", JOptionPane.INFORMATION_MESSAGE);
        }
    }
    this.txtDichVu.setText("");
    this.txtGia.setText("");
}//GEN-LAST:event_btnLuuActionPerformed

private void btnTroLaiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnTroLaiActionPerformed
    // TODO add your handling code here:
    this.txtDichVu.setEnabled(false);
    this.txtGia.setEnabled(false);
    this.btnThem.setEnabled(true);
    this.btnXoa.setEnabled(true);
    this.btnSua.setEnabled(true);
    this.btnLuu.setEnabled(false);
    if(this.jtbDanhSachDV.getSelectedRow()!=-1)
    {
        this.txtDichVu.setText(jtbDanhSachDV.getValueAt(this.jtbDanhSachDV.getSelectedRow(), 1).toString());
        this.txtGia.setText(jtbDanhSachDV.getValueAt(this.jtbDanhSachDV.getSelectedRow(), 2).toString());
    }
    else
    {
        this.txtDichVu.setText("");
        this.txtGia.setText("");
    }
    this.btnTroLai.setEnabled(false);
    flag = 0;
}//GEN-LAST:event_btnTroLaiActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(frmDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(frmDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(frmDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(frmDichVu.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new frmDichVu().setVisible(true);
            }
        });
    }

    private void UpdateTable(JTable jtbDanhSachDV, String[] tableColumnsName, WebRowSet wrs) {
        try{
            DefaultTableModel dfTable = (DefaultTableModel) jtbDanhSachDV.getModel();
            dfTable.getDataVector().removeAllElements();
            dfTable.setColumnIdentifiers(tableColumnsName);
            while(wrs.next()){
                rowdata = new Vector();
                rowdata.add("MDV"+wrs.getString("MaDV"));
                rowdata.add(wrs.getString("TenDV"));
                rowdata.add(Format_Price(wrs.getString("Gia").replace(".0", "")));
                dfTable.addRow(rowdata);
            }
            jtbDanhSachDV.setModel(dfTable);
            jtbDanhSachDV.getColumnModel().getColumn(0).setPreferredWidth(30);
            jtbDanhSachDV.getColumnModel().getColumn(2).setPreferredWidth(30);
        }
        catch(Exception e)
        {
            System.out.println(e);
        }
    }

    ///<editor-fold defaultstate="collapsed" desc=" Format_Price ">
    private String Format_Price(String Price)
        {
            if ("0".equals(Price))
            {
                return "0";
            }
            else
                Price = Price.replace(".0", "");
            Price = Price.replace(",", "");
            String tmp = "";
            while (Price.length() > 3)
            {
                tmp = "." + Price.substring(Price.length() - 3) + tmp;
                Price = Price.substring(0, Price.length() - 3);
            }
            tmp = Price + tmp;
            return tmp;
        }
    //</editor-fold>
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnLuu;
    private javax.swing.JButton btnPrint;
    private javax.swing.JButton btnSua;
    private javax.swing.JButton btnThem;
    private javax.swing.JButton btnTroLai;
    private javax.swing.JButton btnXoa;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jtbDanhSachDV;
    private javax.swing.JTextField txtDichVu;
    private javax.swing.JTextField txtGia;
    // End of variables declaration//GEN-END:variables

    
}
